package com.xyzq.hs;

import com.xyzq.hs.util.ByteUtils;
import com.xyzq.hs.util.Logger;
import com.xyzq.hs.util.LoggerFactory;

public class ByteReader {
	
	Logger log=LoggerFactory.getLogger(ByteReader.class);
	
	private int pos;
	
	private byte[] bytes;

	public ByteReader(byte[] bytes) {
		super();
		this.bytes = bytes;
		this.pos=0;
	}
	
	public byte[] read(int len){
		log.debug("pos:{}",pos);
		byte[] result = ByteUtils.subBytes(bytes, this.pos, len);
		if(log.isDebug()){
			ByteUtils.println("read", result);
		}
		this.pos+=len;
		return result;
	}
	
	public short readShort(){
		return ByteUtils.byte2short(read(2));
	}
	
	
	public int readInt(int len){
		return ByteUtils.byte2int(read(4));
	}
	
	public String readStr(int len){
		//string end '\0'
		len-=1;
		byte[] result = read(len);
		this.pos+=1;
		return ByteUtils.byte2str(result);
	}
	
	public char[] readChars(int len){
		return ByteUtils.byte2char(read(len));
	}
	
	public char readChar(){
		return ByteUtils.byte2char(read(1))[0];
	}

}
